package com.mida.bms.report.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.mida.bms.report.entity.DocCustomTextJson;
import com.mida.bms.report.entity.DocStructReport;
import com.mida.bms.report.mapper.DocCustomTextJsonMapper;
import com.mida.bms.report.mapper.DocStructReportMapper;
import com.mida.bms.report.service.DocSupportAreaService;
import com.mida.bms.report.vo.DocStructParagraphVo;
import com.mida.bms.report.vo.DocStructRequestVo;
import com.mida.bms.report.vo.DocStructVo;
import com.mida.bms.report.vo.PatientInfoParameterVo;
import com.mida.tool.common.result.BaseResult;
import com.mida.tool.common.result.ListResult;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 佐证区
 */
@Service
@Slf4j
@Transactional
public class DocSupportAreaServiceImpl extends BaseResult implements DocSupportAreaService {

    @Autowired
    private DocStructReportMapper docStructReportMapper;

    /**
     * 根据人标识以及 检查类型查询相关检查详情列表
     *
     * @param vo
     * @return
     * @throws Exception
     */
    @Override
    public ListResult<List<DocStructRequestVo>> getDocSupportContentList(
            PatientInfoParameterVo vo) throws Exception {

        Map<String, Object> mapParam = new HashMap<>();
        List<DocStructRequestVo> ls = new ArrayList<>();
        ListResult<List<DocStructRequestVo>> lr = new ListResult<>();
        DocStructRequestVo docStructRequestVo = new DocStructRequestVo();
        if (ObjectUtils.isEmpty(vo) && ObjectUtils.isEmpty(vo.getPersonSoid())) {
            log.error("患者标识不能为空");
            buildListWarn(lr, "患者标识不能为空");
            return lr;
        }
        mapParam.put("apntPersonSoid", vo.getPersonSoid());
        if (vo.getExamServiceTypeSoid() != null) {
            List<Long> listExamType = new ArrayList<Long>();
            listExamType.add(Long.valueOf(vo.getExamServiceTypeSoid()));
            mapParam.put("listExamType", listExamType);
        }
        mapParam.put("allStatus","allStatus");
        List<DocStructReport> list = docStructReportMapper.getHistoryReportList(mapParam);
        if (list == null) {
            return lr;
        }

        List<List<DocStructVo>> lists  = new ArrayList<>();
        List<DocStructVo> docReportVoList = null;
        if (list != null) {
            for (DocStructReport d : list) {
                String reportContent = d.getReportContent();
                docReportVoList = JSONObject.parseArray(reportContent, DocStructVo.class);
                lists.add(docReportVoList);
            }
        }

        for (int i = 0; i < lists.size(); i++) {
            //处理第一个模板数据，只保留特殊段落
            //过滤前三个段落，和最后一个
            List<DocStructParagraphVo> docStructParagraphVoList = lists.get(i).get(0).getDocStructParagraphVoList();
            //模板段落数有问题，直接返回空
            if (docStructParagraphVoList.size() < 4) {
                buildListSuccess(lr, null);
                return lr;
            }
            docStructParagraphVoList.remove(docStructParagraphVoList.size() - 1);

            for (int j = 0; j <= 2; j++) {
                docStructParagraphVoList.remove(0);
            }

            List<DocStructParagraphVo> docStructParagraphVoListResult = new ArrayList<>();

            for (DocStructVo docStructVo : docReportVoList) {
                if (ObjectUtils.isNotEmpty(docStructVo) && CollectionUtils.isNotEmpty(docStructVo.getDocStructParagraphVoList())) {
                    docStructParagraphVoListResult.addAll(docStructVo.getDocStructParagraphVoList());
                }
            }

            DocStructVo docStructVo = new DocStructVo();
            docStructVo.setDocStructParagraphVoList(docStructParagraphVoListResult);
            docStructVo.setExamTypeSoid(list.get(i).getExamType());
            if (list.get(i).getCreateDate() != null) {
                SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
                docStructVo.setExamDate(sdf.format(list.get(i).getCreateDate()));
            }
            docStructRequestVo.setDocStructVo(docStructVo);

            ls.add(docStructRequestVo);

        }
        buildListSuccess(lr, ls);
        return lr;
    }
}
